$.Class("StatisticSite.Grid",
// static properties
{         
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/statisticSite/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    }
                },
                change: function (e, data) {									
                    if(e.action == "sync")
                        $("#grid").data("kendoGrid").dataSource.read();	
                },
                error: function (xhr, error) {
                    //console.debug(xhr); console.debug(error);
                },				
                batch: true,
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    },
                        model: {
                                id: "id",
                                fields: {
                                        id: { editable: false},
                                        domain: { validation: { required: true }},
                                        ip: { validation: { required: true }},
                                        planType: {defaultValue:'Very short'},
                                        profitType: {defaultValue:'Low'},
                                        status: {defaultValue:'New'},
                                        startDate: {},
                                        endDate: {},							
                                        startDomain: {},
                                        endDomain: {},
                                        joinedDate: {},				
                                        plan: {},
                                        joinedDateNumber:{},
                                        lastPaid: {},
                                        lastWithdraw: {},
                                        rate: {},
                                        sslType: {defaultValue:0},
                                        budget:{}
                                }
                        }
                },							
                requestStart: function(e) {   
                        var data = {
                        domain : $("#_domain").val(),
                        status : $("#_status").val(),
                        planType : $("#_planType").val(),
                        ip : $("#_ip").val(),
                        monitorDomain : $("#_monitorDomain").val(),
                        paymentMethod : $("[name='_paymentMethod']:checked").val(),
                        isAjax : true
                    };
                    
                    StatisticSite.Grid.addParams(e, data);
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["create","Reset"],
	        //height: 500,
	        filterable: false,	        
	        pageable: true,
	        columns: [{
                    field:"grid_index",
                    title:" ",
                    width: 40
                },{
                    field:"domain",
                    title:"Domain",
                    width: 180,
                },{
                    field:"planTime",
                    title:"Plan time ",
                    width: 120
                },{
                    field:"monitor",
                    title:"Monitor",
                    width: 80
                },{
                    field:"adv",
                    title:"Adv",
                    width: 80
                },{
                    field:"payment",
                    title:"Payment",
                    width: 80
                },{
                    field:"feature",
                    title:"Feature",
                    width: 80
                },{
                    field:"startDate",
                    title:"Start date",
                    width: 100
                },{
                    field:"endDate",
                    title:"End date",
                    width: 100
                },{
                    field:"lifeTime",
                    title:"Life time",
                    width: 80
                },{
                    field:"id",
                    title:" ",
                    width: 60,
                    template: '<a href="/statisticSite/edit?id=${id}">Edit</a>'
                },{
                    field:"id",
                    title:" ",
                    width: 70,
                    template: '<a href="" onclick="return StatisticSite.delete(${id});">Delete</a>'
                },{
                    title:"",
                    field:"",   
                    template: ''
                }],
            sortable:false,
            selectable: 'row',
            dataBound: function(e) {                
            }
        });	
        },
        initEvents : function(id)
        {           
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        
            $(".k-grid-add", "#" + this.id).click(function(){
                document.location = "/StatisticSite/create";
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

StatisticSite.delete = function(id)
{
    if(confirm("Do you want to delete?"))
    {
        $.ajax({
            type: 'post',
            url: '/statisticSite/delete',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}